package com.sunday.pinxinghui.model.mine;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.event.EventBus;
import com.sunday.common.event.ExitApp;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.CacheUtils;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.UIAlertView;
import com.sunday.pinxinghui.MainActivity;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.model.login.LoginActivity;
import com.sunday.pinxinghui.network.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/24.
 */

public class SettingActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.menu_clear_cash)
    TextView menuClearCash;
    @Bind(R.id.btn_logout)
    TextView btnLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        titleView.setText("设置");
        menuClearCash.setText("清理缓存  （" + CacheUtils.getTotalCacheSize(mContext) + "）");
    }


    @OnClick({R.id.menu_clear_cash, R.id.btn_logout})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_clear_cash:
                CacheUtils.clearAllCache(mContext);
                menuClearCash.setText("清理缓存  （" + CacheUtils.getTotalCacheSize(mContext) + "）");
                break;
            case R.id.btn_logout:
                showLogoutDialog();
                break;
        }
    }

    private void showLogoutDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("温馨提示");
        builder.setMessage("退出登录？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                deleteClientId();
                //finish();

            }
        });
        builder.show();

    }

    private void deleteClientId() {
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().deleteClientId(BaseApplication.getInstance().getMember().getId());
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    SharePerferenceUtils.getIns(mContext).removeKey("oAuth");
                    SharePerferenceUtils.getIns(mContext).removeKey(Constants.IS_LOGIN);
                    SharePerferenceUtils.getIns(mContext).removeKey(Constants.MEMBERID);
                    EventBus.getDefault().post(new ExitApp());
                    intent = new Intent(mContext, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }


}
